/** 
 * Copyright 2010 Erlacher Felix, Estgfaeller Wolfgang, Ferula Patrick
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package at.socialconference.app.connection;

import android.content.Context;
import at.socialconference.app.model.AUser;
import at.socialconference.app.persistence.Settings;
import at.socialconference.inter.connection.SocketConnector;
import at.socialconference.inter.connection.protocol.SocketProtocol;
import at.socialconference.inter.model.Conference;
import at.socialconference.inter.model.User;

/**
 * Handles Connection to Backend Server
 * @author Patrick Ferula
 */
public class ConnectionHandler {

	private static SocketConnector connector;
	
	public static void init(Context cxt){
		connector = new SocketConnector(Settings.getServerURL(cxt));
	}
	
	/**
	 * Connects to Server
	 * @param cxt Context of Application - for username and password from settings
	 * @return true if success - false if error
	 */
	public static boolean connect(Context cxt) {
		return connect(Settings.getUsername(cxt), Settings.getPassword(cxt));
		
	}
	
	public static boolean connect(String username, String password) {
		try {
			int status = connector.connect(username, password);
			return status == SocketProtocol.OK;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	public static Conference getCurrentConference() {
		try {
			return connector.getConferenceNow();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public static Object handleQRCode(String contents) {
		try {
			return connector.handleQRCode(contents);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static AUser getUser() {
		try {
			User u = connector.getUser(); 
			return new AUser(u);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static int checkLogin(String username, String password) {
		try{
			return connector.checkLogin(username, password);
		}catch (Exception e) {
			return -1;
		}
	}

	public static int friendRequest(Integer id) {
		try{
			return connector.friendRequest(id);
		}catch (Exception e) {
			return -1;
		}
	}

	public static int checkFriendStatus(Integer id) {
		try{
			return connector.checkFriendStatus(id);
		}catch (Exception e) {
			return -1; //NOFRIEND
		}
	}

	public static boolean participatesToConference(Integer id) {
		try{
			return connector.isEnrolled(id);
		}catch (Exception e) {
			return false; //NOFRIEND
		}
	}
	
	public static int enroll(Integer id) {
		try{
			return connector.enroll(id);
		}catch (Exception e) {
			return -1; //FAILURE
		}
	}

	public static int addTwitterTokens(String tw_at, String tw_at_secret) {
		try{
			return connector.addTwitterTokens(tw_at, tw_at_secret);
		}catch (Exception e) {
			return -1;
		}
	}


}
